<template>
  <div>
      <div style="width: 400px; margin: 150px auto;">
          <h1 style="text-align: center; margin-bottom: 30px">注册</h1>
          <el-form :model="user" size="large" :rules="rules" ref="ruleFormRef">
            <!-- <el-form-item style="text-align: center" label-width="0">
              <el-upload
                class="avatar-uploader"
                action="api/sys/headerImgUpload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
              <img v-if="user.image" :src = "user.image" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </el-form-item> -->
              <el-form-item prop="name">
                  <el-input v-model="user.name" :prefix-icon="UserFilled"/>
              </el-form-item>
              <el-form-item prop="password">
                  <el-input v-model="user.password" :prefix-icon="Lock" show-password/>
              </el-form-item>
              <el-form-item prop="confirm">
                  <el-input v-model="user.confirm" :prefix-icon="Lock" show-password/>
              </el-form-item>
              <el-form-item prop="phone">
                  <el-input v-model="user.phone" :prefix-icon="Iphone"/>
              </el-form-item>
              <el-form-item prop="sex" label="性别">
                  <el-radio-group v-model="user.sex">
                    <el-radio label="1" size="large">男</el-radio>
                    <el-radio label="0" size="large">女</el-radio>
                    <el-radio label="2" size="large">未知</el-radio>
                  </el-radio-group>
              </el-form-item>
              <el-form-item>
                  <el-button type="primary" style="width: 100%" @click="register">注册</el-button>
              </el-form-item>
          </el-form>
      </div>
  </div>
</template>

<script setup>
    import { ref, reactive, getCurrentInstance } from 'vue'
    import {UserFilled, Lock, Iphone} from '@element-plus/icons-vue'
    import {ElMessage} from "element-plus";
    import request from "../utils/request";
    const { proxy } = getCurrentInstance()

    const user = reactive({
      status: 0,
    })

    const rules = reactive({
        name: [
            { required: true, message: '请输入用户名！', trigger: 'blur' }
        ],
        password: [
            { required: true, message: '请输入密码！', trigger: 'blur' }
        ],
        confirm: [
            { required: true, message: '请确认密码！', trigger: 'blur' }
        ],
        phone: [
            { required: true, message: '请输入电话号码！', trigger: 'blur' }
        ],
    })

    const register = () => {
      if(user.password != user.confirm){
        ElMessage({
            type: "error",
            message: "两次密码不一致"
        })
          return
      }
      proxy.$refs.ruleFormRef.validate((valid) => {
        if(valid){
          request.post("/user/register", user).then(res => {
            if(res.code === 200){
              ElMessage({
                type: "success",
                message: "注册成功"
              })
              proxy.$router.push('/login')
            }else{
              ElMessage({
                type: "error",
                message: res.msg
              })
            }
          })
        }else{
          ElMessage({
            type: "error",
            message: ref.msg
          })

        }
      })
    }
    
    

</script>

<style>

</style>